﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBUtlity;
using Models;
using System.Data;
using System.Data.SqlClient;


namespace DAL
{
    public class PJLineDebugService
    {
        #region 新增
        public int InsertInfo(PJLineDebug main)
        {
            string sql = "insert into PJLineDebugs";
            sql += "(WorkParentId,SerialNum,OrderType,LineNum,OrderStartSerialNum)";
            sql += " values(@WorkParentId,@SerialNum,@OrderType,@LineNum,@OrderStartSerialNum)";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",main.WorkParentId),
                new SqlParameter("@SerialNum",main.SerialNum),
                new SqlParameter("@OrderType",main.OrderType),
                new SqlParameter("@LineNum",main.LineNum),
                new SqlParameter("@OrderStartSerialNum",main.OrderStartSerialNum)
            };
            return SQLHelper.Update(sql, param);
        }
        #endregion

        #region 更改
        public int UpdateInfo(PJLineDebug main)
        {
            string sql = "update PJLineDebugs set";
            sql += " LineNum=@LineNum where WorkParentId=@WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",main.WorkParentId),
                new SqlParameter("@LineNum",main.LineNum)
            };
            return SQLHelper.Update(sql, param);
        }
        public int UpdateSapAsset(string sapAsset, int workParentId)
        {
            string sql = "update PJLineDebugs set SapAsset=@SapAsset";
            sql += " where WorkParentId=@WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@SapAsset",sapAsset),
                new SqlParameter("@WorkParentId",workParentId)
            };
            return SQLHelper.Update(sql, param);
        }
        public int UpdateSapBill(string sapBill, int workParentId)
        {
            string sql = "update PJLineDebugs set SapBill=@SapBill";
            sql += " where WorkParentId=@WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@SapBill",sapBill),
                new SqlParameter("@WorkParentId",workParentId)
            };
            return SQLHelper.Update(sql, param);
        }
        public int UpdateStatues(int workParentId)
        {
            string sql = "update PJLineDebugs set Statues=@Statues";
            sql += " where WorkParentId=@WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@Statues","1"),
                new SqlParameter("@WorkParentId",workParentId)
            };
            return SQLHelper.Update(sql, param);
        }
        #endregion
        #region 查询
        /// <summary>
        /// 获取最大Id行的流水号
        /// </summary>
        /// <returns></returns>
        public string GetLastSerialNum(string orderType, string right2)
        {
            string sql = "select SerialNum from PJLineDebugs";
            sql += " where Id=(select Max(Id) from PJLineDebugs";
            sql += " where OrderType=@OrderType and right(OrderStartSerialNum,2)=@Right2)";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@OrderType",orderType),
                new SqlParameter("@Right2",right2)
            };
            return Convert.ToString(SQLHelper.GetSingleResult(sql, param));
        }
        public PJLineDebug GetLineDebug(int workParentId)
        {
            string sql = "select * from PJLineDebugs where WorkParentId=@WorkParentId";
            PJLineDebug objPJLineDebug = null;
            SqlDataReader objReader = SQLHelper.GetReader(sql, new SqlParameter[] { new SqlParameter("@WorkParentId", workParentId) });
            if (objReader.Read())
            {
                objPJLineDebug = new PJLineDebug()
                {
                    Id = Convert.ToInt32(objReader["Id"]),
                    WorkParentId = Convert.ToInt32(objReader["WorkParentId"]),
                    SerialNum = objReader["SerialNum"].ToString(),
                    OrderType = objReader["OrderType"].ToString(),
                    LineNum = objReader["LineNum"].ToString(),
                    OrderStartSerialNum = objReader["OrderStartSerialNum"].ToString(),
                    SapAsset = objReader["SapAsset"].ToString(),
                    SapBill = objReader["SapBill"].ToString(),
                    Statues = objReader["Statues"].ToString() == "0" ? "未同步" : "已同步"
                };
            }
            objReader.Close();
            return objPJLineDebug;
        }
        public int IsNoLineDebug(int workParentId)
        {
            string sql = "select count(*) from PJLineDebugs where WorkParentId=@WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",workParentId)
            };
            return Convert.ToInt32(SQLHelper.GetSingleResult(sql, param));
        }

        #endregion
    }
}
